package ihm.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import tools.ImageUtils;

/**
 * Composite object that draw an Icon and the corresponding username. The
 * composite has the following style: 
 * ________ 
 * |       | 
 * | photo |
 * |       |
 * |_______|
 * UserName
 * 
 * @author Nicolas Yzet <nicolas.yzet@gmail.com>
 * 
 */
public class Element extends Composite {

	private final int MAX_HEIGHT = 200;
	private final int MAX_WIDTH = 100;

	private Color borderColor;
	private Label imgContainer;
	private Label usernameContainer;

	private Image profilePicture;

	/**
	 * Construct a User composite from a username and an Icon.
	 * 
	 * @param parent
	 *            composite.
	 * @param username
	 *            to display in the caption.
	 * @param picture
	 *            user picture.
	 */
	public Element(Composite parent, String username, Image picture) {
		super(parent, SWT.NONE);

		// ////////////////////////////////////////////
		// general composite style
		// ///////////////////////////////////////////

		RowLayout layout = new RowLayout();
		layout.type = SWT.VERTICAL;
		layout.center = true;
		layout.marginWidth = 1;
		layout.marginHeight = 1;
		setLayout(layout);

		this.borderColor = getBackground();
		this.addPaintListener(new PaintListener() {
			@Override
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(borderColor);
				e.gc.drawRectangle(0, 0, getBounds().width - 1,
						getBounds().height - 1);
			}
		});

		// ///////////////////////////////////////////////
		// Set controls data
		// ///////////////////////////////////////////////
		imgContainer = new Label(this, SWT.CENTER);
		imgContainer.setSize(MAX_WIDTH, MAX_HEIGHT);

		ImageData picData = ImageUtils.scaleImage(picture, MAX_HEIGHT,
				MAX_WIDTH);
		profilePicture = new Image(getDisplay(), picData);
		imgContainer.setImage(profilePicture);

		usernameContainer = new Label(this, SWT.CENTER);
		usernameContainer.setText(username);

		// /////////////////////////////////////////////////////////////////
		// Setup event handling
		// ///////////////////////////////////////////////////////

		MouseTrackAdapter mouseTrackAdapter = new MouseTrackAdapter() {

			@Override
			public void mouseExit(MouseEvent e) {
				setBorderColor(getBackground());
			}

			@Override
			public void mouseEnter(MouseEvent e) {
				setBorderColor(getDisplay().getSystemColor(
						SWT.COLOR_TITLE_BACKGROUND));
			}
		};

		usernameContainer.addMouseTrackListener(mouseTrackAdapter);
		imgContainer.addMouseTrackListener(mouseTrackAdapter);
	}

	/**
	 * Mouse events are fired when the image or the caption is clicked.
	 * 
	 * @param listener
	 *            Mouse event listener
	 */
	@Override
	public void addMouseListener(MouseListener listener) {
		usernameContainer.addMouseListener(listener);
		imgContainer.addMouseListener(listener);
	}

	private void setBorderColor(Color color) {
		this.borderColor = color;
		redraw();
	}

	public String getUserName() {
		return this.usernameContainer.getText();
	}

	public Image getPicture() {
		return this.profilePicture;
	}

}
